﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Text;

using Ordinaire.Commerce.Purchasing.Db;
using Ordinaire.Commerce.Purchasing.Data;

namespace Ordinaire.Commerce.Purchasing
{
    public class PurchasingDataManager : JunctionDataManager<PContext>
    {
        #region overrides

        /// <summary>
        /// Returns the preferred DataLoadOptions for current DataManager. This DataLoadOptions will be
        /// used by DataManager.Context when Context is instantiated.
        /// </summary>
        /// <returns>the DataLoadOptions set for current DataManager</returns>
        public override DataLoadOptions GetDataLoadOptions()
        {
            var loadOptions = new DataLoadOptions();
            loadOptions.LoadWith<Purchases>(d => d.PurchaseItems);
            return loadOptions;
        }

        #endregion

        #region Public methods

        /// <summary>
        /// Returns purchase with the specified id.
        /// </summary>
        /// <param name="id">the purchase id</param>
        /// <returns>the purchase</returns>
        public Purchases Get(int id)
        {
            return Get<Purchases, int, string>(id);
        }

        /// <summary>
        /// Inserts new Purchases data with the specified date, organisation/person id, and serial number.
        /// </summary>
        /// <param name="date">the invoice date</param>
        /// <param name="organisationId">the organisation id where the goods was purchased from</param>
        /// <param name="personId">the person id whom the goods was purchased from</param>
        /// <param name="serialNo">optional purchase serial number</param>
        /// <returns>the created data</returns>
        public Purchases InsertPurchases(DateTime date, int organisationId, int personId, string serialNo)
        {
            Purchases newData = new Purchases {
                Date            = date,
                SerialNo        = serialNo,
                OrganisationId  = (organisationId <= 0) ? (int ?) null : organisationId,
                PersonId        = (personId <= 0) ? (int ?) null :  personId
            };

            var results = Insert<Purchases, int, string>(newData);
            return results[0];
        }

        #endregion
    }
}
